/**
 * Project Name:MeatballClient.java
 * File Name:MeatballClient.java
 * Date:2018/8/26 下午2:13
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.token.sdk.http;

import com.alibaba.fastjson.JSON;
import com.meatball.token.sdk.model.TokenResultMsg;
import com.meatball.token.sdk.util.MD5Util;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: MeatballClient.java
 * @Description: todo(Meatball权限工具类)
 * @Author 張翔宇
 * @Date 2018/8/26 下午2:13
 * @Version V1.0
 */
public class MeatballClient {

    /**
     * @Title: acquireToken
     * @Description: todo(token请求)
     * @Params: appId
     * @Params: appsecret
     * @Params: host
     * @Return     返回类型
     * @Author 張翔宇
     * @Date 2018/8/26 下午5:48
     */
    public static TokenResultMsg acquireToken(String appId, String appsecret, String host) {

        String timestamp = String.valueOf(System.currentTimeMillis());
        Map<String, String> params = new HashMap<String, String>(3);
        params.put("appId", appId);
        params.put("sign", sign(appId, appsecret, timestamp));
        params.put("timestamp", timestamp);
        TokenResultMsg resultMsg = JSON.parseObject(HttpHelper.sendPost(host + "/api/token/getaccesstoken",
                JSON.toJSONString(params), false), TokenResultMsg.class);
        return resultMsg;
    }

    /**
     * @Title: verifyToken
     * @Description: todo(验证token是否正确)
     * @Params: appId
     * @Params: appsecret
     * @Params: host
     * @Params: token
     * @Return     返回类型
     * @Author 張翔宇
     * @Date 2018/8/27 下午9:26
     */
    public static TokenResultMsg verifyToken(String appId, String appsecret, String host, String token) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        Map<String, String> params = new HashMap<String, String>(3);
        params.put("appId", appId);
        params.put("sign", sign(appId, appsecret, timestamp));
        params.put("timestamp", timestamp);
        params.put("access_token", token);
        TokenResultMsg resultMsg = JSON.parseObject(HttpHelper.sendPost(host + "/api/token/verifyAccess_token",
                JSON.toJSONString(params), false), TokenResultMsg.class);
        return resultMsg;
    }

    /**
     * @Title: sign
     * @Description: todo(签名)
     * @Params: appId
     * @Params: appsecret
     * @Params: timestamp
     * @Return     返回类型
     * @Author 張翔宇
     * @Date 2018/8/28 上午8:39
     */
    private static String sign (String appId, String appsecret, String timestamp) {
        Map<String, String> sign = new HashMap<String, String>(3);
        sign.put("appId", appId);
        sign.put("appsecret", appsecret);
        sign.put("timestamp", timestamp);
        return MD5Util.MD5Encode(JSON.toJSONString(sign));
    }

    public static void main(String[] args) {
        String host = "http://39.104.134.114";
        String appId = "mt3c053a275251a779";
        String appsecret = "51c35ccfde5ce125704c12431c65832f";

        // 请求
        TokenResultMsg resultMsg = MeatballClient.acquireToken(appId, appsecret, host);
        System.out.println("Message: " + resultMsg.getMessage());
        System.out.println("Token: " + resultMsg.getAccessToken());

        // 验证
        TokenResultMsg tokenResultMsg = MeatballClient.verifyToken(appId, appsecret, host, resultMsg.getAccessToken());
        System.out.println("Message: " + tokenResultMsg.getMessage());
        System.out.println("Token: " + tokenResultMsg.getAccessToken());

    }
}
